<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="请假类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option
            v-for="item in dataForm.typeList"
            :key="item.key"
            :label="item.name"
            :value="item.key"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="原因" prop="reason">
        <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="dataForm.reason"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="dataForm.startTime"
          @change="handelEndTime"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="dataForm.endTime"
          @change="handelEndTime"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="请假时长" prop="totalTime">
        <el-input v-model="dataForm.totalTime" placeholder="请假时长" :disabled="true"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        type: "",
        title: "",
        reason: "",
        startTime: "",
        endTime: "",
        totalTime: "",
        typeList: []
      },
      dataRule: {
        type: [
          { required: true, message: "请假类型不能为空", trigger: "blur" }
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        reason: [{ required: true, message: "原因不能为空", trigger: "blur" }],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/activiti/leave/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.leave.id;
              this.dataForm.type = data.leave.type;
              this.dataForm.title = data.leave.title;
              this.dataForm.reason = data.leave.reason;
              this.dataForm.startTime = data.leave.startTime;
              this.dataForm.endTime = data.leave.endTime;
              this.dataForm.totalTime = data.leave.totalTime;
            }
          });
        }

        this.$http({
          url: this.$http.adornUrl(`/activiti/leave/getInstance`),
          method: "get",
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataForm.typeList = data.list;
          }
        });
      });
    },

    handelEndTime() {
      let { startTime, endTime } = this.dataForm;
      if (startTime && endTime) {
        let stattTimeDate = new Date(startTime).getTime();
        let endTimeDate = new Date(endTime).getTime();
        if (stattTimeDate > endTimeDate) {
          this.$message({
            message: "开始时间不能大于结束时间",
            type: "error",
            duration: 1500
          });
          this.dataForm.endTime = "";
        }
        let date = endTimeDate - stattTimeDate;
        var days = date / 1000 / 60 / 60 / 24;
        var daysRound = Math.floor(days);
        var hours = date / 1000 / 60 / 60 - 24 * daysRound;
        var hoursRound = Math.floor(hours);
        var minutes = date / 1000 / 60 - 24 * 60 * daysRound - 60 * hoursRound;
        var minutesRound = Math.floor(minutes);
        this.dataForm.totalTime = `${daysRound}天${hoursRound}小时${minutesRound}分`;
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/activiti/leave/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              type: this.dataForm.type,
              title: this.dataForm.title,
              reason: this.dataForm.reason,
              startTime: this.dataForm.startTime,
              endTime: this.dataForm.endTime,
              totalTime: this.dataForm.totalTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
